package com.ebt.m.account;

import android.os.Handler;
import android.os.Message;

import com.ebt.m.utils.ConfigData;
import com.ebt.m.utils.StringUtils;
import com.ebt.m.exception.EBTException.EBTSoapHeaderException;
import com.ebt.m.utils.android.WebServiceRequestHelper;

import org.apache.http.HttpException;
import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


/**
 * AccoutManager about user account
 *
 * @author Allen.li
 */
public class AccountServiceManager {
    public static final String PHONE = "Phone";
    public static final String MAIL = "Mail";


    public static final int MSG_ACCOUNT_OK = 1;
    public static final int ACCOUNT_PWD_ERROR = 2;
    public static final int ACCOUNT_NO_USER = 3;

    public static final int ACCOUNT_CHECK_ERROR = -1;
    public static final int PWD_CHANGE_ERROR = -2;

    public AccountServiceManager() {

    }

    /**
     * 重置密码
     *
     * @param userName
     * @param newPwd
     * @param handler
     */
    public void resetPassword(String userName, String newPwd, Handler handler) {
        Message msg = handler.obtainMessage();

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("UserName", userName);
        params.put("NewPassWord", newPwd);
        Map<String, String> map = getStrJsonParamsWithServiceAndParams("resetPwd", params);

        try {
            String info = WebServiceRequestHelper.sendRequestByNoAccount(map, ConfigData.URL_UAC_WSDL, ConfigData.URL_UAC_ACTION, ConfigData.URL_UAC_METHOD, ConfigData.URL_UAC_XMLNS);
            if (info != null && info.length() != 0) {
                // Log.i("---changePassword info--",info);
                JSONObject json = new JSONObject(info);
                boolean result = json.optBoolean("result", false);
                if (result) {
                    msg.what = MSG_ACCOUNT_OK;
                } else {
                    msg.what = PWD_CHANGE_ERROR;
                }
            } else {// 返回信息有问题，接口问题
                msg.what = PWD_CHANGE_ERROR;
            }

        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
            msg.what = AccountConstant.EXCEPTION_CONNECT_TIMEOUT;
        } catch (HttpException e) {
            e.printStackTrace();
            msg.what = AccountConstant.EXCEPTION_CONNECT_FAILED;
        } catch (EBTSoapHeaderException e) {
            e.printStackTrace();
            msg.what = AccountConstant.EXCEPTION_EBTSoapHeader;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            msg.what = AccountConstant.EXCEPTION_CONNECT_FAILED;
        } catch (Exception e) {
            e.printStackTrace();
            msg.what = AccountConstant.EXCEPTION;
        }
        handler.sendMessage(msg);
    }

    /**
     * 获取绑定手机号或者邮箱
     *
     * @param handler
     * @return
     */
    public void getBindPhoneOrEmailByUserName(String UserName, Handler handler) {
        Message msg = handler.obtainMessage();
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("UserName", UserName);
        params.put("ReqFrom", "EBTClient");
        Map<String, String> map = getStrJsonParamsWithServiceAndParams("getUserInfo", params);
        try {
            String info = WebServiceRequestHelper.sendRequestByNoAccount(map, ConfigData.URL_UAC_WSDL, ConfigData.URL_UAC_ACTION,
                    ConfigData.URL_UAC_METHOD, ConfigData.URL_UAC_XMLNS);
            if (info != null && info.length() != 0) {
                String[] phone_email = new String[2];
                try {
                    JSONObject json = new JSONObject(info);
                    String phone = json.optString("Phone");
                    String email = json.optString("Email");
                    if (StringUtils.isEmpty(phone) && StringUtils.isEmpty(email)) {
                        msg.what = AccountConstant.EXCEPTION;
                    } else {
                        if (!StringUtils.isEmpty(phone)) {
                            phone_email[0] = phone;
                        }
                        if (!StringUtils.isEmpty(email)) {
                            phone_email[1] = email;
                        }
                        msg.what = MSG_ACCOUNT_OK;
                        msg.obj = phone_email;
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    msg.what = AccountConstant.EXCEPTION;
                }
            } else {// 返回信息有问题，接口问题
                msg.what = AccountConstant.EXCEPTION;
            }

        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
            msg.what = AccountConstant.EXCEPTION_CONNECT_TIMEOUT;
        } catch (HttpException e) {
            e.printStackTrace();
            msg.what = AccountConstant.EXCEPTION_CONNECT_FAILED;
        } catch (EBTSoapHeaderException e) {
            e.printStackTrace();
            msg.what = AccountConstant.EXCEPTION_EBTSoapHeader;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            msg.what = AccountConstant.EXCEPTION_CONNECT_FAILED;
        } catch (Exception e) {
            e.printStackTrace();
            msg.what = AccountConstant.EXCEPTION;
        }
        handler.sendMessage(msg);
    }

    public static Map<String, String> getStrJsonParamsWithServiceAndParams(String serviceName,
                                                                           HashMap<String, String> paramsMap) {
        JSONObject json = new JSONObject();
        JSONArray array = new JSONArray();
        try {
            json.put("ServiceName", serviceName);
            Set<Map.Entry<String, String>> set = paramsMap.entrySet();
            Iterator<Map.Entry<String, String>> it = set.iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> keyValue = it.next();
                array.put(new JSONObject().put("name", keyValue.getKey()).put("value", keyValue.getValue()));
                json.put("Parameters", array);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Map<String, String> pamps = new HashMap<String, String>();
        pamps.put("strJson", json.toString());

        return pamps;
    }

}
